import { getGoodsListSearch } from "../../request/goods";
import { Event } from "../../utils/interface";

export interface Good {
  add_time: string;
  cat_id: number;
  cat_one_id: number;
  cat_three_id: number;
  cat_two_id: number;
  goods_big_logo: string;
  goods_id: number;
  goods_name: string;
  goods_number: number;
  goods_price: number;
  goods_small_logo: string;
  goods_weight: number;
  hot_mumber: number;
  is_promote: boolean;
  upd_time: number;
}

//当前状态
type State = undefined | "loading" | "end";
const headers = ["综合", "销量", "价格"];
let total = 66;
let goods: Array<Good> = [];
let pagenum = 1;
let pagesize = 7;
let sort = 0;
let query = "";
let state: State;

Component({
  properties: {},
  data: {
    goods,
    pagenum,
    pagesize,
    query,
    headers,
    sort,
    state,
  },
  methods: {
    //封装获取数据模块
    async getData(reset?: boolean) {
      const result = await getGoodsListSearch({
        query: this.data.query ? this.data.query : "创维",
        pagenum: reset ? 1 : pagenum,
        pagesize: reset ? pagenum * pagesize : pagesize,
      });
      total = result.total;
      return result;
    },
    //初始化
    async initGoods() {
      const result = await this.getData();
      goods = result.goods;
      this.setData({ total, goods, pagenum });
    },
    //改变排序方式
    changeSort(e: any) {
      const index = e.currentTarget.dataset.i;
      if (index === this.data.sort) return;
      if (index === 2) {
        this.sortPrice();
      } else if (index === 1) {
        this.sortCount();
      } else {
        this.sortDefaut();
      }
      sort = index;
      this.setData({ sort, goods });
    },
    copyArr(arr: Array<Good>) {
      return arr.map((item) => ({ ...item }));
    },
    //下滑加载数据
    async sliceGoods() {
      if (state === "end") return;
      state = "loading";
      this.setData({ state });
      pagenum++;
      const result = await this.getData();
      state = undefined;
      result.goods.length || (state = "end");
      goods = [...goods, ...result.goods];
      if (this.data.sort === 2) {
        this.sortPrice();
      }
      if (this.data.sort === 1) {
        goods.reverse();
      }
      this.setData({ pagenum, goods, state });
    },
    toDetails,
    /**
     * @sort
     * 排序方式
     */
    sortPrice() {
      for (let i = 0; i < goods.length - 1; i++) {
        for (let j = 0; j < goods.length - 1 - i; j++) {
          let max: any;
          if (goods[j + 1].goods_price > goods[j].goods_price) {
            max = goods[j + 1];
            goods[j + 1] = goods[j];
            goods[j] = max;
          }
        }
      }
    },
    sortCount() {
      goods.reverse();
    },
    async sortDefaut() {
      const result = await this.getData(true);
      goods = result.goods;
      this.setData({ goods });
    },
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
});
function toDetails(e: Event<{ row: Good }>) {
  wx.navigateTo({
    url: `/pages/goods_detail/goods_detail?goods_id=${e.currentTarget.dataset.row.goods_id}`,
  });
}
